package com.tianye.salon.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tianye.salon.exception.MyException;
import com.tianye.salon.model.AdminModule;
import com.tianye.salon.model.AdminRole;
import com.tianye.salon.service.AdminModuleService;
import com.tianye.salon.service.AdminRoleService;



@Controller
@RequestMapping("/admin-role")
public class RoleController {
	protected Logger logger = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private AdminRoleService adminRoleService;
	@Autowired
	private AdminModuleService adminModuleService;
	
	@RequestMapping("index")
	public ModelAndView index() throws Exception{
		List<AdminRole> roles = adminRoleService.selectAll();
		
		
		ModelAndView modelAndView = new ModelAndView("/admin/role");
		modelAndView.addObject("roles", roles);
		
		
		return modelAndView;
	}
	
	@RequestMapping("add")
	public ModelAndView add() throws Exception {
		ModelAndView mav = new ModelAndView("/admin/role_add");
		
		List<AdminModule> modules = adminModuleService.selectAll();
		
		mav.addObject("modules", modules);
		
		return mav;
	}
	
	@RequestMapping("update")
	public ModelAndView update(String id,HttpSession session) throws Exception {
		//System.out.println("id is:"+id);
		//PtRole role = new PtRole();
		AdminRole role = adminRoleService.selectRoleModuleByPrimaryKey(Integer.parseInt(id));
		ModelAndView mav = new ModelAndView("/admin/role_update");
		
		List<AdminModule> modules = adminModuleService.selectAll();
		
		mav.addObject("role", role);
		mav.addObject("modules", modules);
		
		return mav;
	}
	
	@RequestMapping(value="ajaxadd",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String add(String roleName, String roleDescription, String mids, HttpSession session) throws Exception{
		logger.info("roleName:"+roleName);
		logger.info("roleDescription:"+roleDescription);
		logger.info("mids:"+mids);
		roleName = roleName.trim();
		roleDescription = roleDescription.trim();
		mids = mids.trim();
		
		try {
			if(roleName.equals("")){
				throw new MyException("角色名称不能为空！");
			} else if(roleDescription.equals("")){
				throw new MyException("角色描述不能为空！");
			} else if(mids.equals("")){
				throw new MyException("请先选择角色模块！");
			} else {
				String[] midsList = mids.split("\\|");
				int midint = 0;
				
				List<AdminModule> modules = new ArrayList<AdminModule>();
				for(String mid: midsList){
					try {
						midint = Integer.parseInt(mid);
						
						AdminModule module = adminModuleService.selectById(midint);
						if(null == module){
							throw new MyException("模块id【"+mid+"】不存在！");
						}
						modules.add(module);
					} catch (NumberFormatException e) {
						e.printStackTrace();
						throw new MyException("模块id【"+mid+"】错误！");
					}
				}
				
				AdminRole role = new AdminRole();
				role.setName(roleName);
				role.setDescription(roleDescription);
				role.setModules(modules);
				
				adminRoleService.insertRoleAndModules(role);
			}
		} catch (MyException e) {
			return e.getMessage();
		}
		
		return "ok";
	}
	
	@RequestMapping(value="ajaxupdate", produces="application/json; charset=utf-8")
	@ResponseBody
	public String update(int roleId, String roleName, String roleDescription, String mids, HttpSession session) throws Exception{
		logger.info("roleId:"+roleId);
		logger.info("roleName:"+roleName);
		logger.info("roleDescription:"+roleDescription);
		logger.info("mids:"+mids);
		roleName = roleName.trim();
		roleDescription = roleDescription.trim();
		mids = mids.trim();
		
		try {
			if(roleId == 0){
				throw new MyException("角色id错误！");
			} else if(roleName.equals("")){
				throw new MyException("角色名称不能为空！");
			} else if(roleDescription.equals("")){
				throw new MyException("角色描述不能为空！");
			} else if(mids.equals("")){
				throw new MyException("请先选择角色模块！");
			} else {
				String[] midsList = mids.split("\\|");
				int midint = 0;
				
				List<AdminModule> modules = new ArrayList<AdminModule>();
				for(String mid: midsList){
					try {
						midint = Integer.parseInt(mid);
						
						AdminModule module = adminModuleService.selectById(midint);
						if(null == module){
							throw new MyException("模块id【"+mid+"】不存在！");
						}
						modules.add(module);
					} catch (NumberFormatException e) {
						e.printStackTrace();
						throw new MyException("模块id【"+mid+"】错误！");
					}
				}
				
				AdminRole role = adminRoleService.selectById(roleId);
				if(null == role){
					throw new MyException("未找到待修改角色【"+roleName+"】！");
				}
				
				role.setName(roleName);
				role.setDescription(roleDescription);
				role.setModules(modules);
				
				adminRoleService.deleteRoleModulesByRoleid(roleId);
				adminRoleService.updateRoleModules(role);
			}
		} catch (MyException e) {
			return e.getMessage();
		}
		
		return "ok";
	}
	
	@RequestMapping(value="ajaxdelete", produces="application/json; charset=utf-8")
	@ResponseBody
	public String delete(int roleId){
		adminRoleService.deleteRoleModules(roleId);
		
		return "ok";
	}
}
